<template>
  <div v-if="visible" class="modal-overlay" @click.self="handleClose">
    <div class="modal-content">
      <div class="modal-header">
        <h3>创建新页面</h3>
        <button class="close-btn" @click="handleClose">
          <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
            <path d="M15.8333 4.16667L4.16666 15.8333M4.16666 4.16667L15.8333 15.8333" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </button>
      </div>
      
      <div class="modal-body">
        <div class="form-group">
          <label for="new-page-name">页面名称</label>
          <input
            id="new-page-name"
            type="text"
            v-model="pageName"
            class="form-input"
            placeholder="请输入页面名称"
            ref="pageNameInput"
          />
          <div v-if="errorMessage" class="error-message">{{ errorMessage }}</div>
        </div>
      </div>
      
      <div class="modal-footer">
        <button class="btn-cancel" @click="handleClose">取消</button>
        <button class="btn-create" @click="handleCreate" :disabled="!isValid">创建</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineEmits, ref, computed, onMounted, nextTick,watch} from 'vue';

// Props定义
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
});


// Emits定义
const emit = defineEmits(['close', 'create']);
// 内部状态
const pageName = ref('');
const errorMessage = ref('');
const pageNameInput = ref(null);

// 监听visible变化，当模态框显示时自动聚焦输入框
watch(
  () => props.visible,
  (newVisible) => {
    if (newVisible) {
      resetState();
      nextTick(() => {
        if (pageNameInput.value) {
          pageNameInput.value.focus();
        }
      });
    }
  }
);

// 验证页面名称是否有效
const isValid = computed(() => {
  return pageName.value.trim().length > 0;
});

// 重置状态
const resetState = () => {
  pageName.value = '';
  errorMessage.value = '';
};

// 关闭模态框
const handleClose = () => {
  emit('close');
};

// 创建新页面
const handleCreate = () => {
  const name = pageName.value.trim();
  if (name == '') {
    errorMessage.value = '请输入页面名称';
    return;
  }
  
  if (name.length > 50) {
    errorMessage.value = '页面名称不能超过50个字符';
    return;
  }
  
  // 检查是否包含特殊字符
  const invalidChars = /[<>"'/\|?*%&^!@#$`]/;
  if (invalidChars.test(name)) {
    errorMessage.value = '页面名称不能包含特殊字符';
    return;
  }
  
  emit('create', name);
};
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease;
}

.modal-content {
  background-color: white;
  border-radius: 8px;
  width: 100%;
  max-width: 400px;
  overflow: hidden;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  animation: slideUp 0.3s ease;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #e2e8f0;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #334155;
}

.close-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: #64748b;
  padding: 4px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.close-btn:hover {
  background-color: #f1f5f9;
}

.modal-body {
  padding: 20px;
}

.form-group {
  margin-bottom: 0;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-size: 14px;
  font-weight: 500;
  color: #334155;
}

.form-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.2s;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #165dff;
  box-shadow: 0 0 0 3px rgba(22, 93, 255, 0.1);
}

.error-message {
  margin-top: 4px;
  font-size: 12px;
  color: #ef4444;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px;
  border-top: 1px solid #e2e8f0;
  background-color: #f8fafc;
}

.btn-cancel,
.btn-create {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-cancel {
  background-color: #f3f4f6;
  color: #334155;
}

.btn-cancel:hover {
  background-color: #e5e7eb;
}

.btn-create {
  background-color: #165dff;
  color: white;
}

.btn-create:hover:not(:disabled) {
  background-color: #0d47a1;
}

.btn-create:disabled {
  background-color: #9ca3af;
  cursor: not-allowed;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideUp {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}
</style>